import React, {Component} from 'react';
import {PageContainer} from "@ant-design/pro-components";
import {FillApi, FillApi2, SchoolPass, SchoolPassAll} from "@/services/School/SchoolApi";
import {Button, message, Space, Table, Tag, Modal} from "antd";
import {ServerMessages} from "@/services/ServerMessages";
import {Link} from "umi";
import schoolCheck from "../../../../config/map/schoolCheck";
import {history} from "umi";
import {ShowError} from "@/utils/format";
import {ExclamationCircleOutlined} from "@ant-design/icons";

const { confirm } = Modal;


class Items extends Component {

  storage=window.localStorage

  state = {
    loading: true,
    data: [],
    current_page: parseInt(this.storage["school_check_current_page"]) ? parseInt(this.storage["school_check_current_page"]) : 1,
    page_size: parseInt(this.storage["school_check_page_size"]) ? parseInt(this.storage["school_check_page_size"]) : 10,
  }

  componentDidMount() {
    const result = new URLSearchParams(this.props.location.search);
    const param = result.get('name');
    console.log("@get参数", param)

    const queryString = new URLSearchParams({discipline_name: param}).toString()
    FillApi2('/api/index/school-query/query-single-discipline-current', queryString).then(response => {
      console.log(response)
      if (response.status === 0) {
        // const docx_map = ['3_1_1docx','1_1_1','2_2_2_2','5_4_2','2_1_1docx','5_4_1']
        const docx_map = []
        let tmp_data = []
        for (let i = 0; i < response.data.length; i++) {
          if (docx_map.indexOf(response.data[i].fill_id) < 0) {
            tmp_data.push(response.data[i])
          }
        }
        this.setState({loading: false, data: tmp_data})
      } else {
        ShowError(response.message)
        history.goBack()
      }
    })

    // FillApi('/api/index/school-query/query-single-discipline-current', {
    //   'discipline_name': param,
    // }, (response) => {
    //   console.log(response)
    //   const docx_map = ['3_1_1docx','1_1_1','2_2_2_2','5_4_2','2_1_1docx','5_4_1']
    //   let tmp_data = []
    //   for (let i = 0; i < response.data.length; i++) {
    //     if (docx_map.indexOf(response.data[i].fill_id) < 0) {
    //       tmp_data.push(response.data[i])
    //     }
    //   }
    //   this.setState({loading: false, data: tmp_data})
    // }, (response) => {
    //   ShowError(response.message)
    // }, () => ShowError(ServerMessages.error_message))

    // const storage=window.localStorage;
    //
    // if (!storage) {
    //   return
    // }
    //
    // const current_page = storage["school_check_current_page"] ? storage["school_check_current_page"] : 1
    // const page_size = storage["school_check_page_size"] ? storage["school_check_page_size"] : 10
    // console.log("@初始化：", current_page, page_size)
    // this.setState({current_page: current_page, page_size: page_size})
  }

  render() {

    const columns = [
      {
        title: '监测点',
        dataIndex: 'fill_about',
        key: 'fill_about',
        render: text => <b>{text}</b>,
      },
      {
        title: '填报周期',
        dataIndex: 'fill_cycle',
        key: 'fill_cycle',
        render: text => <b>{text}</b>
      },
      {
        title: '审核状态',
        dataIndex: 'is_seen',
        key: 'is_seen',
        render: (text) => {
          if (text === 0) {
            return <Tag>未审核</Tag>
          } else if (text === 1) {
            return <Tag color="success">已审核</Tag>
          } else {
            return ""
          }
        }
      },
      {
        title: '操作',
        key: 'action',
        render: (_, record) => {
          console.log("@@record", record)
          return <Space size="middle">
            {/*<Button type="primary" onClick={() => message.success('审核成功')}>审核</Button>*/}
            {/*<Button danger onClick={() => message.success('驳回成功')}>驳回</Button>*/}
            <Link
              to={schoolCheck[record.fill_id]+"?id="+record.id+"&fill_id="+record.fill_id+"&flag=check&is_seen="+record.is_seen+"&name="+history.location.query.name}>
              <Button type="default">查看</Button>
            </Link>
          </Space>
        }
      },
    ];

    return (
      <PageContainer title="请选择审核项目">
        <Button style={{marginBottom: 20}} type="primary" onClick={() => {

          confirm({
            title: '确认一键审核?',
            icon: <ExclamationCircleOutlined />,
            content: '请确认您的操作',
            onOk: () => {
              console.log('OK');
              console.log(this.state)
              const {data} = this.state
              let id_arr = []
              for (let i = 0; i < data.length; i++) {
                id_arr.push({id: data[i].id})
              }

              SchoolPassAll(id_arr).then(response => {
                console.log(response)
                if (response.status === 0) {
                  message.success(response.message)
                  location.reload()
                } else {
                  ShowError(response.message)
                }
              })
              // history.goBack()
            },
            onCancel() {
              console.log('Cancel');
            },
          });



        }}>一键审核</Button>
        <Table rowKey="id" onChange={(a) => {
          const current_page = a.current
          const page_size = a.pageSize
          const storage=window.localStorage;

          if (!storage) {
            return
          }

          storage["school_check_current_page"] = current_page
          storage["school_check_page_size"] = page_size

        }} pagination={{defaultCurrent: this.state.current_page, defaultPageSize: this.state.page_size}} loading={this.state.loading} columns={columns} dataSource={this.state.data}/>
      </PageContainer>
    );
  }
}

export default Items;
